package org.beynet.docs.http;

import java.io.File;
import java.text.NumberFormat;
import java.util.Map;
import java.util.Observable;

import org.apache.log4j.Logger;
import org.beynet.docs.sync.FileToSync;

public class DefaultHttpUploaderListener implements HttpUploaderListener {

    private NumberFormat nf;
    
    public void addFileToObserver(FileToSync f) {
        
    }

    public DefaultHttpUploaderListener() {
        nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(0);
    }
    
    @Override
    public void update(Observable arg0, Object arg1) {
        if (arg0 instanceof FileToSync) {
            FileToSync toSync = (FileToSync)arg0;
            String fileName = toSync.getFile().getPath();
            if (arg1==null) {
                System.out.println("New status for file <"+fileName+"> status="+toSync.getStatus().toString());
            }
            else {
                if (FileToSync.Status.IN_PROCESS.equals(toSync.getStatus())) {
                    @SuppressWarnings("unchecked")
                    Map<String,Long> status = (Map<String, Long>) arg1;
                    Long totalSize = status.get(FileToSync.FILE_SIZE);
                    Long sent      = status.get(FileToSync.FILE_SENT);
                    double percent = (sent.longValue()*100.0)/totalSize.longValue() ;
                    System.out.println("File "+fileName+" totalSize="+totalSize+" sent ="+sent+" completed="+nf.format(percent)+" %");
                }
            }
        }
    }
    

    private static final Logger logger = Logger.getLogger(DefaultHttpUploaderListener.class);

}
